package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.wxcomment.WXcommentListVo;
import com.cskaoyan.bean.vo.wxcomment.WxUserCommentVo;
import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.vo.wx.wxcomment.WXCommentCountVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.wx.WXCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author :rain
 * @description
 * @since 2022/06/29 15:25
 */
@Service
public class WXCommentServiceImpl implements WXCommentService {

    @Autowired
    MarketCommentMapper commentMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    /**
     * 获取当前商品的图片数量和当前商品的所有的评论数量
     *
     * @param comment
     * @return
     */
    @Override
    public WXCommentCountVo queryPicAndCommentCount(MarketComment comment) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        //拼接对应的value值
        criteria.andValueIdEqualTo(comment.getValueId());
        //拼接对应的type的值
        criteria.andTypeEqualTo(comment.getType());
        //获取到满足上述属性的list
        List<MarketComment> marketComments = commentMapper.selectByExample(marketCommentExample);
        //统计当前商品的评论数
        int allCommentCount = 0;
        for (MarketComment marketComment : marketComments) {
            allCommentCount++;
        }
        //拼接有图片的信息
        criteria.andHasPictureEqualTo(true);
        //统计有图片的有多少
        List<MarketComment> marketComments1 = commentMapper.selectByExample(marketCommentExample);
        int pictureCount = 0;
        //统计有当前商品有多少评论
        for (MarketComment marketComment : marketComments1) {
            pictureCount++;
        }
        return WXCommentCountVo.getAllInfo(allCommentCount, pictureCount);
    }

    /**
     * 根据当前的showType做出判断，如果当前的showType为0，说明需要返回当前商品的所有评论，包括有图和无图的
     * 如果当前的showType是1，说明需要返回是有图的评论
     *
     * @param comment  valueId 商品的id type=0 是商品的评论
     * @param info     page limit
     * @param showType 0是全部评论， 1是有图的评论
     * @return
     */
    @Override
    public CommonData<WXcommentListVo> queryAllCommentInfo(MarketComment comment, BasePageInfo info, Integer showType) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据商品获取到了当前商品的所有评论信息
        List<WXcommentListVo> commentInfoByGoodsId = commentMapper.selectAllCommentInfoByGoodsId(comment.getValueId());
        //执行的是获取所有的评论
        if (showType == 0) {
            //遍历评论信息
            try {
                for (int i = 0; i < commentInfoByGoodsId.size(); i++) {
                    //获取当前的goodsId
                    Integer goodsId = commentInfoByGoodsId.get(i).getValueId();
                    //根据评论中的商品的id获取到orderGoods的orderId
                    Integer orderId = orderGoodsMapper.selectOrderIdByGoodsId(goodsId);
                    //通过orderId获取到当前order的对象
                    MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
                    //获取到当前order的userId
                    Integer userId = marketOrder.getUserId();
                    //获取user对象
                    MarketUser user = marketUserMapper.selectByPrimaryKey(userId);
                    //生成返回的user对象信息
                    WxUserCommentVo wxUserCommentVo = WXcommentListVo.setUserVo(user.getNickname(), user.getAvatar());
                    //将当前的user对象set进去
                    commentInfoByGoodsId.get(i).setUserInfo(wxUserCommentVo);
                }
            } catch (Exception e) {
                PageInfo<WXcommentListVo> pageInfo = new PageInfo<>(commentInfoByGoodsId);
                return CommonData.data(pageInfo);
            }
            PageInfo<WXcommentListVo> pageInfo = new PageInfo<>(commentInfoByGoodsId);
            return CommonData.data(pageInfo);
        }

        //程序运行到这里，说明showType不为0，则为1，返回的是带有图片的评论

        //过滤没有图片的评论
        List<WXcommentListVo> collect = commentInfoByGoodsId.stream()
                .filter(WXcommentListVo -> WXcommentListVo.getHasPicture() == true)
                .collect(Collectors.toList());
        try {
            for (int i = 0; i < collect.size(); i++) {
                //获取当前的goodsId
                Integer goodsId = collect.get(i).getValueId();
                //根据评论中的商品的id获取到orderGoods的orderId
                Integer orderId = orderGoodsMapper.selectOrderIdByGoodsId(goodsId);
                //通过orderId获取到当前order的对象
                MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
                //获取到当前order的userId
                Integer userId = marketOrder.getUserId();
                //获取user对象
                MarketUser user = marketUserMapper.selectByPrimaryKey(userId);
                //生成返回的user对象信息
                WxUserCommentVo wxUserCommentVo = WXcommentListVo.setUserVo(user.getNickname(), user.getAvatar());
                //将当前的user对象set进去
                collect.get(i).setUserInfo(wxUserCommentVo);
            }
        } catch (Exception e) {
            PageInfo<WXcommentListVo> pageInfo = new PageInfo<>(collect);
            return CommonData.data(pageInfo);
        }
        PageInfo<WXcommentListVo> pageInfo = new PageInfo<>(collect);
        return CommonData.data(pageInfo);

    }

    /**
     * 专题评论中的post方法，type为1，为专题评论
     * 当前请求载体中缺少数据，userId，addTime，updateTime
     * 返回数据中有当前评论的id，需要在sql中计入useGenerate
     * @param comment
     * @return 返回当前出入数据的对象
     */
    @Override
    public MarketComment topicComment(MarketComment comment, MarketUser user) {
        //设置其他关键信息
        comment = setTopicOtherInfo(comment,user);
        int affactRows = commentMapper.insertSelective(comment);
        //插入失败
        if (affactRows == 0){
            return null;
        }
        //通过返回的id获取当前的新增的对象
        MarketComment marketComment = commentMapper.selectByPrimaryKey(comment.getId());
        return marketComment;
    }

    /**
     * 设置话题的其他信息
     * @param comment 当前请求对象
     * @param user  当前登录的用户
     * @return
     */
    private MarketComment setTopicOtherInfo(MarketComment comment, MarketUser user) {
        comment.setDeleted(false);
        comment.setAddTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setUserId(user.getId());
        return comment;
    }
}
